<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建对象的方式</title>
</head>
<body>
    <script>
        // 1. 对象字面量
        let obj = {
            name: 'zhangsan',
            age: 18,
            sayHello() {
                console.log('hello');
            }
        }
        console.log(obj);
        obj.sayHello();

        // 2. new Object()
        let obj2 = new Object();
        obj2.name = 'lisi';
        obj2.age = 20;
        console.log(obj2);

        //3. 工厂方法
        function createObj(name, age) {
            return {
                name: name,
                age: age,
                sayHello() {
                    console.log('hello');
                }
            }
        }
        let obj3 = createObj('wangwu', 25);
        console.log(obj3);

        // 4. 构造函数
        function Person(name, age) {
            this.name = name;
            this.age = age;
            this.sayHello = function() {
                console.log('hello');
            }
        }
        let obj4 = new Person('wangwu', 25);
        console.log(obj4);
    </script>
</body>
</html>